import { _decorator, Button, find, Label, tween, Tween, UITransform, Widget } from 'cc';
import { Ab_DataTransfer_bA } from '../../../A-FRAME/A-LIB/lib.b.data';
import { Ab_BaseUI_bA } from '../../../A-FRAME/component/ui.base';
import Ab_GameDC_bA from '../../manager/data.game.center';
import Ab_GameAssets_bA from '../../manager/game.assets';
import { Ab_Float_bA } from './float';
import { Ab_GameBoard_bA } from './game.board';
import { Ab_Popup_bA } from './popup';
import { Ab_Guide_bA } from './guide';
const { ccclass, property } = _decorator;

@ccclass('Ab_Game_bA')
export class Ab_Game_bA extends Ab_BaseUI_bA {
	@property(Ab_GameBoard_bA)
	private Ab_board_bA: Ab_GameBoard_bA = null;

	private Ab_ui_bottom_bA: UITransform;

	private Ab_va_bottom_bA: abi.Ab_cc_bA.Ab_VecAssist_bA;

	private Ab_prop_used_bA: `${'shuffle'|'stash'|'revoke'}-${'video'|'diamond'}`[] = [];

	protected Ab_initAfterOnLoad_bA(){
		this.Ab_ui_bottom_bA = this.node.getChildByName('bottom').getComponent(UITransform);
		this.Ab_va_bottom_bA = new abi.Ab_cc_bA.Ab_VecAssist_bA(this.Ab_ui_bottom_bA.node);

		this.Ab_renderLocalUI_bA();
	}

	protected start(){
		let wgt = this.node.getComponent(Widget);
		if(wgt) wgt.updateAlignment();

		this.Ab_va_bottom_bA.Ab_getPosition_bA('Ab_center_bA', 'Ab_over_bottom_bA');

		this.Ab_ui_bottom_bA.node.setPosition(this.Ab_va_bottom_bA.Ab_vec_bA);
		this.node.active = false;
	}

	protected Ab__click_event__bA: { [name: string]: (this: Ab_Game_bA, button: Button) => void; } = {
		async stash(btn){
			this.Ab_enablePropBtn_bA(false);

			let prop = Ab_GameDC_bA.Ab_User_bA.Ab_prop_stash_bA;
			if(prop>0) Ab_GameDC_bA.Ab_Trigger_bA.Ab_STASH_bA = false;
			else{
				let list = this.Ab_prop_used_bA.filter(p=>p.startsWith('stash'));
				if(list.length<2){
					let type = await Ab_Popup_bA.Ab_Prop_bA('stash', list.map(el=>el.replace('stash-', '')) as ['video'|'diamond']);
					if(type != 'not'){
						let count = await Ab_Float_bA.Ab_moveProp_bA('stash', btn.node.getChildByName('hint'));
						Ab_GameDC_bA.Ab_User_bA.Ab_prop_stash_bA += count;
						if(count>0) this.Ab_prop_used_bA.push(`stash-${type}`);
					}
					prop = Ab_GameDC_bA.Ab_User_bA.Ab_prop_stash_bA;

					if(prop>0) Ab_Float_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(62));
				}else Ab_Float_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(61));
			}

			this.Ab_enablePropBtn_bA(true);
		},
		async revoke(btn){
			this.Ab_enablePropBtn_bA(false);

			let prop = Ab_GameDC_bA.Ab_User_bA.Ab_prop_revoke_bA;
			if(prop>0) Ab_GameDC_bA.Ab_Trigger_bA.Ab_REVOKE_bA = false;
			else{
				let list = this.Ab_prop_used_bA.filter(p=>p.startsWith('revoke'));
				if(list.length<2){
					let type = await Ab_Popup_bA.Ab_Prop_bA('revoke', list.map(el=>el.replace('revoke-', '')) as ['video'|'diamond']);
					if(type != 'not'){
						let count = await Ab_Float_bA.Ab_moveProp_bA('revoke', btn.node.getChildByName('hint'));
						Ab_GameDC_bA.Ab_User_bA.Ab_prop_revoke_bA += count;
						if(count>0) this.Ab_prop_used_bA.push(`revoke-${type}`);
					}
					prop = Ab_GameDC_bA.Ab_User_bA.Ab_prop_revoke_bA;

					if(prop>0) Ab_Float_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(62));
				}else Ab_Float_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(61));
			}

			this.Ab_enablePropBtn_bA(true);
		},
		async shuffle(btn){
			this.Ab_enablePropBtn_bA(false);

			let prop = Ab_GameDC_bA.Ab_User_bA.Ab_prop_shuffle_bA;
			if(prop>0) Ab_GameDC_bA.Ab_Trigger_bA.Ab_SHUFFLE_bA = false;
			else{
				let list = this.Ab_prop_used_bA.filter(p=>p.startsWith('shuffle'));
				if(list.length<2){
					let type = await Ab_Popup_bA.Ab_Prop_bA('shuffle', list.map(el=>el.replace('shuffle-', '')) as ['video'|'diamond']);
					if(type != 'not'){
						let count = await Ab_Float_bA.Ab_moveProp_bA('shuffle', btn.node.getChildByName('hint'));
						Ab_GameDC_bA.Ab_User_bA.Ab_prop_shuffle_bA += count;
						if(count>0) this.Ab_prop_used_bA.push(`shuffle-${type}`);
					}
					prop = Ab_GameDC_bA.Ab_User_bA.Ab_prop_shuffle_bA;

					if(prop>0) Ab_Float_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(62));
				}else Ab_Float_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(61));
			}

			this.Ab_enablePropBtn_bA(true);
		},
		async drawer_unlock(btn){
			btn.interactable = false;
			await Ab_Popup_bA.Ab_DrawerLock_bA();
			btn.interactable = true;
		}
	};

	protected Ab_listening_bA(){
		let [lbl_stash, lbl_revoke, lbl_shuffle] = ['stash', 'revoke', 'shuffle']
		.map(name=>this.Ab_ui_bottom_bA.node.getChildByName('lbl-value-'+name).getComponent(Label));

		let [icon_add_stash, icon_add_revoke, icon_add_shuffle] = ['stash', 'revoke', 'shuffle']
		.map(name=>find(name+'/hint-add', this.Ab_ui_bottom_bA.node));

		//this.Ab___btn_map___bA.get('drawer_unlock').node.active = Ab_GameDC_bA.Ab_User_bA.Ab_lock_bA;
		this.Ab___btn_map___bA.get('drawer_unlock').node.active = false;

		let list = [];
		list.push(
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_UIGame_bA', (_, show)=>{
				if(_==show) return void 0;
				if(show) this.Ab_enter_bA().then(()=>Ab_GameDC_bA.Ab_User_bA.Ab_round_bA>1 ? this.Ab_initGuide_bA() : void 0);
				else this.Ab_leave_bA();
			}, false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_LoadRound_bA', (_, round)=>this.Ab_loadRound_bA(round), false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_prop_stash_bA', (o, v)=>{
				let empty = !(v>0);
				icon_add_stash.active = empty;
				lbl_stash.node.active = !empty;
				if(!empty) lbl_stash.string = String(v);
			}),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_prop_revoke_bA', (o, v)=>{
				let empty = !(v>0);
				icon_add_revoke.active = empty;
				lbl_revoke.node.active = !empty;
				if(!empty) lbl_revoke.string = String(v);
			}),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_prop_shuffle_bA', (o, v)=>{
				let empty = !(v>0);
				icon_add_shuffle.active = empty;
				lbl_shuffle.node.active = !empty;
				if(!empty) lbl_shuffle.string = String(v);
			}),
			//Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_USE_PROP_bA', (_, p)=>this.Ab_prop_used_bA.push(p)),
			//Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_lock_bA', (_, lock)=>this.Ab___btn_map___bA.get('drawer_unlock').node.active = lock, false)
		);

		return list;
	}

	protected Ab_enter_bA(duration?: number){
		duration = duration ?? 0.5;
		let np = new abi.Ab_NudityPromise_bA<'end'>();

		this.node.active = true;

		this.Ab_va_bottom_bA.Ab_getPosition_bA('Ab_center_bA', 'Ab_bottom_bA');

		Tween.stopAllByTarget(this.Ab_ui_bottom_bA.node);

		tween(this.Ab_ui_bottom_bA.node)
		.to(duration, {position: this.Ab_va_bottom_bA.Ab_vec_bA}, {easing: 'cubicOut'})
		.call(()=>np.Ab_resolve_bA('end'))
		.start();

		return np.Ab_promise_bA;
	}

	protected Ab_leave_bA(duration?: number){
		duration = duration ?? 0.5;

		this.Ab_va_bottom_bA.Ab_getPosition_bA('Ab_center_bA', 'Ab_over_bottom_bA');

		Tween.stopAllByTarget(this.Ab_ui_bottom_bA.node);

		tween(this.Ab_ui_bottom_bA.node)
		.to(duration, {position: this.Ab_va_bottom_bA.Ab_vec_bA}, {easing: 'cubicIn'})
		.call(()=>this.Ab_loadRound_bA(Ab_GameDC_bA.Ab_User_bA.Ab_round_bA))
		.start();
	}

	private Ab_renderLocalUI_bA(){
		(<const>[
			['stash', 49],
			['revoke', 50],
			['shuffle', 51],
		]).forEach(([name, index])=>this.Ab_ui_bottom_bA.node.getChildByName('lbl-'+name).getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(index));
	}

	/**
	 * 加载关卡
	 * @param round 关卡，从1开始
	 */
	private async Ab_loadRound_bA(round: number){
		round = Math.max(1, Math.min(4, round));
		Ab_GameDC_bA.Ab_Status_bA.Ab_removeTileCount_bA = 0;
		Ab_GameDC_bA.Ab_Status_bA.Ab_gameover_bA = undefined;
		Ab_GameDC_bA.Ab_Status_bA.Ab_resurgence_bA = 1;
		abi.Ab_array_bA.Ab_clear_bA(this.Ab_prop_used_bA);

		await this.Ab_board_bA.Ab_renderBoard_bA(await Ab_GameAssets_bA.Ab_loadRoundData_bA(round, '00'));

		if(round>=4){
			this.Ab_board_bA.Ab_setLoop_bA(await Ab_GameAssets_bA.Ab_loadRoundLayout_bA('level_zero'));
		}
	}

	private Ab_enablePropBtn_bA(enable: boolean){
		['stash', 'revoke', 'shuffle'].forEach(name=>this.Ab___btn_map___bA.get(name).interactable = enable);
	}

	private async Ab_initGuide_bA(){
		if(Ab_GameDC_bA.Ab_checkGuideID_bA(7)) return void 0;

		let np_guide = new abi.Ab_NudityPromise_bA<'end'>();
		if(Ab_GameDC_bA.Ab_Status_bA.Ab_guideInited_bA) np_guide.Ab_resolve_bA('end');
		else{
			let reg = Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Status_bA, 'Ab_guideInited_bA', ()=>{
				Ab_DataTransfer_bA.Ab_cancel_bA(reg);
				np_guide.Ab_resolve_bA('end');
			}, false);
		}
		await np_guide.Ab_promise_bA;

		Ab_Guide_bA.Ab_Create_bA(7,
			Ab_GameAssets_bA.Ab_getLangText_bA(54),
			this.Ab_ui_bottom_bA,
			null
		);

		Ab_Guide_bA.Ab_Wait_bA(6, true).then(()=>Ab_Guide_bA.Ab_Show_bA(7, 2));
	}
}

